package com.hcb.tonguetiphealth.widget.homepage;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.adapter.DishListAdapter;
import com.hcb.tonguetiphealth.adapter.MemberListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.ResDish;
import com.hcb.tonguetiphealth.model.User;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.MemberHomepageActivity;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class FansAttentionListWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "BookOnlineWidget";
	
	private Activity ctx;
	private Handler mHandler;
	private ListView memberListView;
	private MemberListAdapter memberListAdapter;
	private List<User>memberList;
	private String count = "0";
	private LinearLayout llBack;
	
	private String type;
	private String FromWidgetFlag;
	private String keyWord;
	private User user;
	
	private TextView tvTitle;
	
	public FansAttentionListWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_health_fans_attention, this); 
		Bundle bundle = ctx.getIntent().getExtras();
		user = (User)bundle.get("user");
		type = ctx.getIntent().getStringExtra("type");
		FromWidgetFlag = ctx.getIntent().getStringExtra("FromWidgetFlag");
		bindView();
		bindEvent();
		getMember();
	}
	
	private void bindEvent() {

		memberListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				User member = memberList.get(arg2);
				Bundle bundle = new Bundle();
				bundle.putSerializable("memeber", member);
				Intent intent = new Intent(ctx, MemberHomepageActivity.class);
				intent.putExtras(bundle);
			//	ctx.getIntent().putExtra("fromWidgetFlag", "MemberListWidget");
			//	ctx.setContentView(new HomepageMemberWidget(ctx));
				ctx.startActivity(intent);
			}
		});
		
		 
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取列表..");
					break;
				case MessageWhat.DIMISS_DIALOG:
					memberListAdapter = new MemberListAdapter(ctx, memberList);
					memberListView.setAdapter(memberListAdapter);
					dismissMessageDialog();
					break;
				case MessageWhat.NETWORK_FAILD:
					try {
					  dismissMessageDialog();
					} catch (Exception e) {
						// TODO: handle exception
					}
					//Toast.makeText(ctx,  "网络有点不给力哦", Toast.LENGTH_LONG).show();
					showMessage(ctx, R.string.net_faild);
					break;
				}
			}
		};
 	
	}
	
	private void bindView() {
	
		llBack = (LinearLayout)findViewById(R.id.memberlist_back);
		memberListView = (ListView)findViewById(R.id.member_listview);
		tvTitle = (TextView)findViewById(R.id.title_textview);
		
		if ("1".equals(type)) {
			tvTitle.setText(R.string.homepage_member_fans_);
		}else if ("2".equals(type)) {
			tvTitle.setText(R.string.homeoage_member_attention_);
		}
		
		llBack.setOnClickListener(this);
	}
 
	
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.memberlist_back:
			if ("HomepageMemberWidget".equals(FromWidgetFlag)) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("members", user);
				ctx.getIntent().putExtras(bundle);
				ctx.setContentView(new HomepageMemberWidget(ctx));
			}else if ("HomepagePersonalWidget".equals(FromWidgetFlag)) {
				ctx.setContentView(new HomepagePersonalWidget(ctx));
			}
			break;
		default:
			break;
		}
	}
	
	private void getMember(){
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		memberList = new ArrayList<User>();
		new Thread(){
			public void run() {
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("count",count));
				params.add(new BasicNameValuePair("userid",user.getUserId()));
				params.add(new BasicNameValuePair("type",type));
				System.out.println("type"+type);
				System.out.println("user.getUserId()"+user.getUserId());
				String result  = HttpUtils.Post(Global.getServletfansattentionUrl(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+result);
				}
				if (result!=null) {
					parseResponse(result);
					mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
				}else {
					mHandler.sendEmptyMessage(MessageWhat.NETWORK_FAILD);
				}
				
			}
		}.start();
		
	}
 
	//解析返回的结果（json）
	private void parseResponse(String result){
		JSONManager jsonManager = new JSONManager();
		boolean success = jsonManager.isGotJSONObject(result);
		if (success) {
			JSONArray jsonArray = jsonManager.getJsonList();
			for(int i=0; jsonArray != null  && i < jsonArray.length(); i++) {
				try {
					User member = new User();
					JSONObject obj = jsonArray.getJSONObject(i);
					member.setUserId(obj.getString("userid"));
					member.setUserName(obj.getString("username"));
				//	member.setArea(obj.getString("areaname"));
					member.setFans(obj.getString("fensi"));
				member.setAttention(obj.getString("attention"));
					member.setSex(obj.getString("gender"));
						member.setImgUrl(obj.getString("headicon"));
						member.setLabels(obj.getString("tags"));
						member.setInfo(obj.getString("intro"));
						member.setLevel(StringUtils.parseFloat(obj.getString("level")));
//					member.setLevel(level)
					memberList.add(member);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void notifyAdapter() {
		
	}
 
	
}
